FROM ubuntu:22.04

ARG HOST_UID=1000
ARG HOST_GID=1000

RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
        printf '%s\n' \
            "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" \
            "deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse" \
            "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" \
            "deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse" \
            "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" \
            "deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse" \
            "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" \
            "deb-src http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse" \
        > /etc/apt/sources.list && \
        apt-get update && \
        apt-get install -y --no-install-recommends \
            openssh-client vim python3 python3-pip iputils-ping net-tools\
            git openssh-client sshpass\
            curl wget less\
            sudo \
            ca-certificates && \
        apt-get clean && rm -rf /var/lib/apt/lists/*

# 创建与宿主机匹配的用户
RUN groupadd -g $HOST_GID shawn && \
    useradd -m -u $HOST_UID -g $HOST_GID -s /bin/bash shawn && \
    echo "shawn ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# 添加pip3 国内豆瓣源给全局用户
RUN mkdir -p /etc/pip && \
    echo "[global]\nindex-url = https://pypi.doubanio.com/simple/\ntrusted-host = pypi.doubanio.com" > /etc/pip/pip.conf

# 安装 Ansible
RUN pip3 install ansible
RUN pip3 install paramiko

# 如果链接或文件已存在，先删除再创建符号链接
RUN [ -e /usr/bin/python ] && rm -f /usr/bin/python || true && \
    ln -s /usr/bin/python3 /usr/bin/python && \
    [ -e /usr/bin/pip ] && rm -f /usr/bin/pip || true && \
    ln -s /usr/bin/pip3 /usr/bin/pip

USER shawn
RUN mkdir -p /home/shawn/work
WORKDIR /home/shawn/work

# 生成 SSH Key， RSA 4096 bits
RUN mkdir -p /home/shawn/.ssh
RUN ssh-keygen -t rsa -b 4096 -f /home/shawn/.ssh/id_rsa -q -N "" && \
    cat /home/shawn/.ssh/id_rsa.pub >> /home/shawn/.ssh/authorized_keys && \
    chmod 600 /home/shawn/.ssh/authorized_keys

CMD ["tail", "-f", "/dev/null"]

